﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScheduleMaker.DB.Domain;
using ScheduleMaker.DB.Domain.Core;

namespace ScheduleMaker.DB.CoreDAO
{
    public class EquipmentDAO : CoreAbstractDAO<Equipment>
    {
        private AdapterAbstractDAO<Equipment, EquipmentSchool> wrappedAbstractDAOSchool;
        private AdapterAbstractDAO<Equipment, EquipmentUniversity> wrappedAbstractDAOUniversity;
        private EquipmentDAOSchool wrappedDAOSchool;
        private EquipmentDAOUniversity wrappedDAOUniversity;

        public EquipmentDAO(EquipmentDAOSchool wrappedDAOSchool)
        {
            this.wrappedDAOSchool = wrappedDAOSchool;
            this.wrappedAbstractDAOSchool =
                new AdapterAbstractDAO<Equipment, EquipmentSchool>((AbstractDAO<EquipmentSchool>)wrappedDAOSchool);
        }

        public EquipmentDAO(EquipmentDAOUniversity wrappedDAOUniversity)
        {
            this.wrappedDAOUniversity = wrappedDAOUniversity;
            this.wrappedAbstractDAOUniversity =
                new AdapterAbstractDAO<Equipment, EquipmentUniversity>((AbstractDAO<EquipmentUniversity>)wrappedDAOUniversity);
        }

        public override void update(Equipment entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.update(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.update(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped EquipmentDAOSchool nor EquipmentDAOUniversity was initialized");
            }
        }

        public override void save(Equipment entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.save(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.save(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped EquipmentDAOSchool nor EquipmentDAOUniversity was initialized");
            }
        }

        public override void saveOrUpdate(Equipment entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.saveOrUpdate(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.saveOrUpdate(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped EquipmentDAOSchool nor EquipmentDAOUniversity was initialized");
            }
        }

        public override void delete(Equipment entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.delete(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.delete(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped EquipmentDAOSchool nor EquipmentDAOUniversity was initialized");
            }
        }

        public override Equipment getById(int id)
        {
            Equipment entity;
            if (wrappedDAOSchool != null)
            {
                entity = wrappedAbstractDAOSchool.getById(id);
            }
            else if (wrappedDAOUniversity != null)
            {
                entity = wrappedAbstractDAOUniversity.getById(id);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped EquipmentDAOSchool nor EquipmentDAOUniversity was initialized");
            }

            return entity;
        }

        public override IList<Equipment> getAll()
        {
            IList<Equipment> records;
            if (wrappedDAOSchool != null)
            {
                records = wrappedAbstractDAOSchool.getAll();
            }
            else if (wrappedDAOUniversity != null)
            {
                records = wrappedAbstractDAOUniversity.getAll();
            }
            else
            {
                throw new NotImplementedException("Neither wrapped EquipmentDAOSchool nor EquipmentDAOUniversity was initialized");
            }

            return records;
        }
    }
}
